package com.xiexie.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiexie.seckill.exception.GlobalException;
import com.xiexie.seckill.mapper.UserMapper;
import com.xiexie.seckill.pojo.User;
import com.xiexie.seckill.service.IUserService;
import com.xiexie.seckill.utils.CookieUtil;
import com.xiexie.seckill.utils.MD5Util;
import com.xiexie.seckill.utils.UUIDUtil;
import com.xiexie.seckill.vo.RespBeanEnum;
import com.xiexie.seckill.vo.LoginVo;
import com.xiexie.seckill.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  服务实现类
 *
 * @author xiexie
 * @date 2021-07-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @Override
    public RespBean doLogin(LoginVo loginVo,
                            HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
        //    return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        //}
        //if (ValidatorUtil.isMobile(mobile)) {
        //    return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        //}

        User user = userMapper.selectById(mobile);
        if (user == null) {
            //return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        if (!MD5Util.fromPassToDBPass(password, user.getSalt()).equals(user.getPassword())) {
            //return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        String ticket = UUIDUtil.uuid();

        //request.getSession().setAttribute(ticket, user);

        // 将session 存入redis中
        redisTemplate.opsForValue().set("user:" + ticket, user);
        CookieUtil.setCookie(request, response, "userTicket", ticket);

        return RespBean.success();
    }

    /**
     * 根据cookie 获取用户
     * @param ticket
     * @return
     */
    @Override
    public User getUserByCookie(String ticket,
                                HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(ticket)) {
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + ticket);
        if (null != user) {
            CookieUtil.setCookie(request, response, "userTicket", ticket);
        }
        return user;
    }

    /**
     * 更新密码
     * @param ticket
     * @param password
     * @return
     */
    @Override
    public RespBean updatePassword(String ticket,String password,
                                   HttpServletRequest request, HttpServletResponse response) {

        User user = getUserByCookie(ticket, request, response);
        if (null == user) {
            new GlobalException(RespBeanEnum.MOBILE_NOT_FIND_ERROR);
        }
        user.setPassword(MD5Util.fromPassToDBPass(password, user.getSalt()));
        int result = userMapper.updateById(user);
        if (result == 1) {
            // 更新成功删除redis中缓存
            redisTemplate.delete("user:" + ticket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }

}
